package service;

import dao.TeacherDao;
import domain.Teacher;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherService implements TeacherServiceInterface {
    private static final TeacherDao teacherDao = TeacherDao.getInstance();
    private static final TeacherService teacherService = new TeacherService();
    //将唯一构造器设置成私有，阻止在本类外生成本类的对象
    private TeacherService(){
    }
    public static TeacherService getInstance(){
        return teacherService;
    }
    public Collection<Teacher> findAll(List<Condition> conditionList, Pagination pagination)throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = teacherDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return teachers;
    }
    public boolean add(Teacher teacher)throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean added = teacherDao.add(connection,teacher);
        JdbcHelper.close(connection);
        return added;
    }
    public Teacher find(int id)throws SQLException {
        Teacher teacher = TeacherDao.getInstance().find(id);
        return teacher;
    }
    public boolean delete(int id)throws SQLException , BysjException {
        boolean deleted = teacherDao.delete(id);
        return deleted;
    }
    public boolean update(Teacher teacher)throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = teacherDao.update(connection,teacher);
        JdbcHelper.close(connection);
        return updated;
    }
}
